import ACTIONS from '../actions/actionTypes';

import type { ICustomer } from '@/services/customer.d';

interface IinitialState {
  isFetching: boolean;
  requestRes: null | string;
  customerList: ICustomer[];
  customerDetail: null | ICustomer;
}

const initialState: IinitialState = {
  isFetching: false,
  requestRes: null,
  customerList: [
    {
      key: '1',
      name: '胡彦斌',
      age: 32,
      address: '西湖区湖底公园1号',
    },
    {
      key: '2',
      name: '胡彦祖',
      age: 42,
      address: '西湖区湖底公园1号',
    },
  ],
  customerDetail: null,
};

export default function customerReducer(state = initialState, action) {
  switch (action.type) {
    case ACTIONS.INIT_CUSTOMER:
      return Object.assign({}, state, {
        isFetching: false,
        customerList: action.payload,
      });
    case ACTIONS.GET_DETAIL_CUSTOMER:
      return Object.assign({}, state, {
        isFetching: false,
        customerDetail: action.payload,
      });
    case ACTIONS.ADD_CUSTOMER:
      return Object.assign({}, state, {
        isFetching: false,
        customerList: action.payload,
      });
    case ACTIONS.UPDATE_CUSTOMER:
      return Object.assign({}, state, {
        isFetching: false,
        customerList: action.payload,
      });
    case ACTIONS.REMOVE_CUSTOMER:
      return Object.assign({}, state, {
        isFetching: false,
        customerList: action.payload,
      });
    default:
      return state;
  }
}
